<template>
	<view class="goods-detail-container" >
		<!-- //轮播 -->
			<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular="true">
				<swiper-item v-for="(pic,index) in goods_info.pics" :key="index">
						<image class="swiper-img" :src="pic.pics_big" @click="preview(index)"></image>
				</swiper-item>
			</swiper>	
			
		<!-- //信息 -->
			<view class="goods-info-box" v-if="goods_info.goods_price">
				<!-- 价格 -->
				<view class="price">¥ {{goods_info.goods_price}}</view>
				<!-- 主体 -->
				<view class="goods-info-body">
					<view class="goods-info-name">
						{{goods_info.goods_name}}
					</view>
					<view class="goods-store-icon" >
						<uni-icons type="star" size="30" color="darkgray"></uni-icons>
						<text>收藏</text>
					</view>
				</view>
				<!-- 运费 -->
				<view class="yf">快递:免运费 </view>
			</view>	
			
			<rich-text :nodes="goods_info.goods_introduce"> </rich-text>
			
			<view class="goods-carts">
						<uni-goods-nav :options="options" :fill="true" :button-group="buttonGroup" @click="onClick"
							@buttonClick="buttonClick" />	
			</view>
			
			<!-- 添加商品组件 -->
			<view class="goods-nav">
				<uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
			</view>
	</view>
</template>

<script>
	import {mapState,mapMutations,mapGetters} from 'vuex'
	export default {
		data() {
			return {
				goods_info:{
						
				},
				defaultImgUrl:'http://image2.suning.cn/uimg/b2c/newcatentries/0000000000-000000000191938678_1_200x200.jpg',
				options: [ 
					  {
							icon: 'shop',
							text: '店铺',
							info: 0,
							infoBackgroundColor:'#007aff',
							infoColor:"white"
						}, 
						{
							icon: 'cart',
							text: '购物车',
							info: 0
						}],
					    buttonGroup: [{
					      text: '加入购物车',
					      backgroundColor: '#ff0000',
					      color: '#fff'
					    },
					    {
					      text: '立即购买',
					      backgroundColor: '#ffa200',
					      color: '#fff'
					    }
					    ]
			};
		},
		computed:{
			...mapState('m_cart',['cart']),
			...mapGetters('m_cart',['total'])
		},
		watch:{
			total:{
				handler(newVal){
						let shopObj =	this.options.find(item=>item.text=="购物车")
						shopObj.info=newVal
				},
				immediate:true
			   
			}
		}
		,
		onLoad(options){
			const goods_id=options.goods_id
		  this.getGoodDetail(goods_id)
		}, 
		methods:{ 
		...mapMutations('m_cart',['addToCart']),
		async	getGoodDetail(goods_id){
			 const {data:res} =await	uni.$http.get(`api/public/v1/goods/detail`,{goods_id})
			 if(res.meta.status!=200) return uni.$showMsg()
			 res.message.goods_introduce=res.message.goods_introduce.replace(/<img/g,"<img style=display:'block'").replace(/webp/g,'jpg')
			 this.goods_info=res.message
			  
				console.log(res);
			},
			preview(index){
				uni.previewImage({
					current:index,
					urls:this.goods_info.pics.map(item=>item.pics_big)
				})
			},
			onClick (e) {
				    uni.showToast({
				      title: `点击${e.content.text}`,
				      icon: 'none'
				    })
						if(e.content.text=='购物车'){
								uni.switchTab({
									url:'/pages/cart/cart'
								})
						}
						
				  },
					
		buttonClick (e) {
					if(e.content.text=='加入购物车'){
						const goods={
							goods_id:this.goods_info.goods_id,
							goods_name:this.goods_info.goods_name,
							goods_price:this.goods_info.goods_price,
							goods_count:1,
							goods_small_logo:this.goods_info.goods_small_logo,
							goods_status:true
						}
						
						this.addToCart(goods)
						this.options[1].info == goods.goods_number
					}
					
				},
		}
	}
</script>

<style lang="scss">
.goods-detail-container{
	padding-bottom: 50px;
}	
	
swiper{
	height: 750rpx;
	
	.swiper-img{
		width: 100%;
		height: 100%;
	}
}

.goods-info-box{
	font-size: 14px;
	margin: 20px 0;
	padding: 0 10px;
	.price{
		color: #f00000;
		font-size: 20px;
		// padding-left: 10px;
	}
	.goods-info-body{
		display: flex;
		align-items: center;
		justify-content: space-between;
		.goods-info-name{
			padding-right: 10px;
		}
		.goods-store-icon{
			display: flex;
			flex-direction: column;
			align-items: center;
			padding-left: 10px;
			color: darkgray;
			border-left: solid 1px lightgray;
		}
	}
	
	.yf{
		color: gray;
		font-size: 12px;
		margin-top: 10px;
	}
	
}

.example-body {
		padding: 0;
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
	}

	.goods-carts {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		position: fixed;
		left: 0;
		right: 0;
		/* #ifdef H5 */
		left: var(--window-left);
		right: var(--window-right);
		/* #endif */
		bottom: 0;
	}
	.goods-nav{
		position: fixed;
		bottom: 0;
		width: 100%;
	}
</style>
